package com.finiac.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.finiac.model.User;

public class UserDAOImpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@Override
	public void addOrupdateUser(User user) {
		hibernateTemplate.save(user);

	}

	@Override
	public void deleteUser(long id) {
		hibernateTemplate.bulkUpdate("delete User where id="+id);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectUser(String userName, String cryptPassword) {
		List<User> userList= hibernateTemplate.find("from User where userName ='"+ userName +"' and password ='"+cryptPassword+"'");
		return userList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User selectById(long userId) {
		List<User> userList=hibernateTemplate.find("from User where id = "+userId);
		User user = new User();
		for(int i=0;i<userList.size();i++)
		{
			user = userList.get(i);
		}
		return user;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> listUser() {
		return hibernateTemplate.find("from User");
	}

	@Override
	public void updateUser(User user) {
		hibernateTemplate.update(user);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public User selectByUserName(String userName) {
		List<User> userList=hibernateTemplate.find("from User where userName = '"+userName+"'");
		User user = new User();
		for(int i=0;i<userList.size();i++)
		{
			user = userList.get(i);
		}
		return user;
	}

}
